package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.User;
import com.uha.reggie.service.UserService;
import com.uha.reggie.util.SMSUtils;
import com.uha.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/FindAll")
    public  void findAll(){

        List<User> menuList = userService.list();

        System.out.println(menuList);
    }

    @PostMapping("/sendMsg")
    public ReggieResult<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();

        if (StringUtils.isNotBlank(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);
            //调用阿里云提供的短信服务api完成发送短信
            //SMSUtils.sendMessage("Reggie","",phone,code);
            //需要将生成的验证码保存到session
            session.setAttribute(phone,code);
            return ReggieResult.success("手机验证码短信验证成功");
        }

        return ReggieResult.error("手机验证码短信验证失败");
    }

    @PostMapping("/login")
    public ReggieResult<User> login(@RequestBody Map map, HttpSession session){

        log.info("-------------------------------------------------");
        log.info(map.toString());

        String phone = map.get("phone").toString();

        String code = map.get("code").toString();
        //String code = "229562";

        Object attribute = session.getAttribute(phone);

        if (attribute != null && attribute.equals(code)){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            if (user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            return ReggieResult.success(user);
        }
        return ReggieResult.error("验证码错误");
    }




    @PostMapping("/loginout")
    public ReggieResult<String> loginout(HttpServletRequest request){

        request.getSession().removeAttribute("user");

        return ReggieResult.success("退出成功~");

    }



}
